﻿using Contracts;
using Domain;
using Entities;
using Entities.QueryPersist;
using Queries;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using WebApp.Models;

namespace WebApp.Controllers
{
    public class OrdersController : Controller
    {
        // GET: CreateNewOrder
        [HttpGet]
        public ActionResult CreateNewOrder()
        {
            var model = new NewOrder
            {
                OrderNumber = Convert.ToInt32(DateTime.Now.ToString("yyMMdd")),
                Status = OrderStatus.New
            };

            ViewBag.CustomerId = new SelectList(Customers.ByName, "Id", "FullName");

            return View(model);
        }

        [HttpPost]
        public async Task<ActionResult> CreateNewOrder([Bind(Include = "OrderNumber,Status,CustomerId")] NewOrder model)
        {
            if (ModelState.IsValid)
            {
                // Add CreateNewOrder to the Command Bus
                // await Infrastructure.Application.CommandBus.Add(new CreateNewOrder
                //{
                //
                //});

                return RedirectToAction("Index");
            }

            ViewBag.CustomerId = new SelectList(Customers.ByName, "Id", "FullName", model.CustomerId);

            return View(model);
        }

        private Customers Customers => Infrastructure.Application.Query.Execute<Customers>();
    }
}